package com.taomeng.servlet.admin;

import com.taomeng.constant.MessageConstant;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.RespBody;
import com.taomeng.entity.Role;
import com.taomeng.service.RoleService;
import com.taomeng.servlet.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/role/*")
public class RoleServlet extends BaseServlet {
    @Autowired
    private RoleService roleService;

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        PageBean<Role> pageBean = roleService.findByPage(page, limit);

        RespBody respBody = new RespBody();
        if (pageBean != null) {
            respBody.setCode(0);
            respBody.setMsg(MessageConstant.QUERY_SUCCESSFUL);
            respBody.setCount(pageBean.getTotal());
            respBody.setData(pageBean.getList());
        } else {
            respBody.setCode(500);
            respBody.setMsg(MessageConstant.QUERY_FAILS);
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> map = req.getParameterMap();

        Role role = new Role();
        BeanUtils.populate(role, map);

        RespBody respBody = new RespBody();
        if (roleService.add(role)) {
            respBody.setCode(200);
            respBody.setMsg("新增角色成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("新增角色失败");
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long id = Long.parseLong(req.getParameter("id"));

        RespBody respBody = new RespBody();
        if (roleService.delRoleById(id)) {
            respBody.setCode(200);
            respBody.setMsg("删除成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("删除失败");
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> map = req.getParameterMap();

        String roleId = map.get("roleId")[0];
        String[] pers = map.get("pers");
        roleService.updatePermission(roleId, pers);

        RespBody respBody = new RespBody();
        respBody.setCode(200);
        respBody.setMsg("修改成功");
        RespBody.JsonObject(resp, respBody);
    }


    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
